/**
 * 机构汇总 之 【医废登记】模块
 * */

var NO_DATA_MSG = '未查询到数据';
var OTHER_SOURCE_NAME = [];//其他來源名称列表

var MY_SM_WASTE_DELIVERY = (function() {

	// 收集页面查询参数
	function getDetailParams(){
		
		var data = {};
		// 查询年月
		// data.dateYm = $('#year').val() + '' + $('#month').val();
		data.dtFrom = $('#dtFrom').val();
		data.dtTo = $('#dtTo').val();
		// 登记
		
		var $wasteDeliverySec = $('#waste_delivery_sec');
		
//		// 登记（全部、是、否） 
//		var register = $wasteDeliverySec.find('select[name="waste_delivery_register"]').val(); 
//		if ( register != '9' ){
//			data.register = register;
//		}
		// 来源（处置室、）           
		var source = $wasteDeliverySec.find('select[name="source"]').val(); 
		if ( source != '9' ){
			data.source = source;
			if(source == 99){
				//其他器械
				data.otherSourceName = OTHER_SOURCE_NAME;
			}
		}
		// 种类（感染性、损伤性）
		var wasteType = $wasteDeliverySec.find('select[name="wasteType"]').val(); 
		if ( wasteType != '9' ){
			data.wasteType = wasteType;
		}
		// 处置方式       				
		var deliveryType = $wasteDeliverySec.find('select[name="deliveryType"]').val(); 
		if ( deliveryType != '9' ){
			data.deliveryType = deliveryType;
		}
		// 机构ID
		data.institutionId = $('#institutionId').val();
		data.businessSubType = 201;// 业务区分
		return data;
	}


	/**
	 * 查看明细
	 * */
	function loadInstrumentList(){

		WeKnow.loading();

		var $selectInstrumentName = $('#source');
		$selectInstrumentName.empty();

		var params = {};
		params.institutionId = $("#institutionId").val();
		params.catalogId = 15;//来源

		$.ajax({
			type: "POST",
			url: "invs_instrument_disinfect.do/load_source",
			dataType: "json",
			data: params,
			async: true,//异步调用
			success: function(data) {
				WeKnow.closeLoading();
				if (!WeKnow.filterAjaxResult(data)) return false; // session过期check

				if (data.errcode == 0) {
					// 把取到的结果放到
					e = '<option value="9">全部</option>';
					if ( 'sourceList' in data && data.sourceList.length == 0 ){
						// WeKnow.errorMsg('未查询到该诊所的器械·场所数据!');
						$selectInstrumentName.append(e);
						return;
					}

					$(data.sourceList).each(function( idx,_a){
						// e +='<option value="'+_a.id + '">'+ _a.name + '</option>'
						e +='<option value="'+_a.name + '">'+ _a.name + '</option>'
						OTHER_SOURCE_NAME.push(_a.name);
					});
					e += '<option value="99">其他</option>';
					$selectInstrumentName.append(e);

				} else if (data.errcode == -1 ) {
					WeKnow.errorMsg( '未查询到来源数据!!!' );
				} else {
					WeKnow.systemMsg('查询失败，请重新登录后再试');
				}
			}
		});
	}
	
	// add by zhengxb 2019.6.21  end ---------------------------------------------------------
	return {
		loadInstrumentList:loadInstrumentList,
		getDetailParams:getDetailParams
	}
})();

$(document).ready(function() {
	
	// 显示申诉图片
	$('#waste_tbody').on('click','button[name="showAppealAtta"]',function(){
		var $that = $(this);
		var $parentTr = $that.closest('tr');
		
		var appealAtta = $parentTr.data('appeal_atta');
		var institutionId = $('#institutionId').val();
		// 显示交接单图片
		MY_SHOW_IMG.createShowImgModal(institutionId ,appealAtta);
	});
	
	//显示图片
	$('#waste_tbody').on('click','td.show_img',function(){
		var $that = $(this).closest('tr');
		// 模态框显示诊所名，选定的日期
		$('#imageModalTitle').text($('#institution_name_dis').text() + ' ' + $that.data('serial_date'));
		// 显示交接单图片
		MY_SHOW_IMG.createShowImgModal($that.data('institution_id'),$that.data('receipt_img_id'));
	});
	
	var $tHead  = $('#waste_thead');
	var $tbody = $('#waste_tbody');
	$('#waste_delivery_register,#source,#wasteType,#deliveryType').on('change',function(){
		// 数据查询
		var params = MY_SM_WASTE_DELIVERY.getDetailParams();
		MY_WASTE_DELIVERY_COM.loadDetailItem(params,$tHead,$tbody);
	});
	
	// 处理申诉 OR 取消异常   注：申诉处理由于多个页面共用，故提炼成appeal.js
	$('#waste_tbody').on('click','button[name="check"],button[name="closeIllegal"]',function(){
		var $that = $(this);
		var $parentTr = $that.closest('tr');
		
		var illegalId = $parentTr.data('illegal_id');
		var originBtnName= $that.prop('name');
		
		// 数据查询
		var params = MY_SM_WASTE_DELIVERY.getDetailParams();
		var $tHead  = $('#waste_thead');
		var $tbody = $('#waste_tbody');
		// 更新用模态框
		MY_APPEAL.createModalWin
			(illegalId,
					originBtnName,
					function(){ MY_WASTE_DELIVERY_COM.loadDetailItem(params,$tHead,$tbody);},
					function(){ MY_SUMMARIZE.getSummarize();}
			);
	});
});